package data_transfer;

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import data_generator.entity.SensorNode;
import data_generator.entity.Temperature;
import data_generator.xml.XMLConfig;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class DataFetch {

	public static ArrayList<DataEntity> FetchData(SensorNode sn,
			String start_time, String end_time)  {
		String JSONDataString="";
		String urlPath = "http://vs2.upwis.com:8889/measurement/"+sn.getId()+"/rsc.temperature?start="+start_time+"&end="+end_time;
		URL url;
		try {
			url = new URL(urlPath);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			InputStream inputStream = connection.getInputStream();
			Reader reader = new InputStreamReader(inputStream, "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(reader);
			String str = null;
			StringBuffer stringBuffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				stringBuffer.append(str);
			}
			reader.close();
			connection.disconnect();
			JSONDataString = stringBuffer.toString();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(new Frame(), "Connot open connection!","Error",JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(new Frame(), "No data for this request!","Error",JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
		
//		System.out.println(JSONDataString);

		JSONArray jsonArray = JSONArray.fromObject(JSONDataString);
		ArrayList<DataEntity> deList = new ArrayList<DataEntity>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			DataEntity de = new DataEntity();
			de.setM(Integer.parseInt(jsonObject.getString("m")));
			de.setTs(jsonObject.getString("ts"));
			deList.add(de);
			
//----------------------------------------------------------------------------
			Temperature temp = new Temperature(
					jsonObject.getString("ts"),
					Float.parseFloat(jsonObject.getString("m")));
			ArrayList<Temperature> tempList = (ArrayList<Temperature>)XMLConfig.nodeMap.get(sn);
			tempList.add(temp);
			System.out.println(sn.getId());
			System.out.println(sn.getName());
			System.out.println(temp.getTemperature());
			System.out.println(temp.getTime());
			System.out.println("\n");
			
//------------------------------------------------------------------------------			
			
		}

//		for (int i = 0; i < deList.size(); i++) {
//			System.out.println(deList.get(i).getM());
//			System.out.println(deList.get(i).getTs());
//			System.out.println("\n");
//		}
		return deList;
	}
}
